package com.matace.keychain.presentation.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import com.matace.keychain.R;

import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.lifecycle.ViewModelProvider;
import com.matace.keychain.data.MainPageRepositoryImpl;
import com.matace.keychain.domain.MainPageUseCase;
import com.matace.keychain.presentation.viewmodel.MainViewModel;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private MainViewModel mainViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        mainViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
            @Override
            public <T extends androidx.lifecycle.ViewModel> T create(Class<T> modelClass) {
                if (modelClass.isAssignableFrom(MainViewModel.class)) {
                    MainPageRepositoryImpl userRepository = new MainPageRepositoryImpl();
                    MainPageUseCase mainPageUseCase = new MainPageUseCase(userRepository);
                    return (T) new MainViewModel(mainPageUseCase);
                }
                throw new IllegalArgumentException("Unknown ViewModel class");
            }
        }).get(MainViewModel.class);

        // 观察导航消息
        mainViewModel.getNavigationMessage().observe(this, message -> {
            if (message != null) {
//                Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
                // message 是功能 学习  设置  我的 空
                switch (message) {
                    case "功能":
                        // 如果 message 是 "功能"，执行此代码块
                        Toast.makeText(this, "function", Toast.LENGTH_SHORT).show();
                        break;
                    case "学习":
                        // 如果 message 是 "学习"，执行此代码块
                        Toast.makeText(this, "learn", Toast.LENGTH_SHORT).show();
                        break;
                    case "设置":
                        // 如果 message 是 "设置"，执行此代码块
                        Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
                        break;
                    case "我的":
                        // 如果 message 是 "我的"，执行此代码块
                        Toast.makeText(this, "my", Toast.LENGTH_SHORT).show();
                        // 跳转到我的页面
                        Intent intent = new Intent(this, AccountActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                    default:
                        // 如果 message 不匹配上述任何值，执行此代码块
                        Toast.makeText(this, "unkonw", Toast.LENGTH_SHORT).show();
                        break;
                }
                
            }
        });

        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            return mainViewModel.handleNavigationItemSelected(itemId);
        });
    }
}